﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.NhanVien;
using DevExpress.XtraEditors.Repository;
using RTSoft.SoureCode.Control.Quyen;
using RTSoft.SoureCode.Control.NhomQuyen;
using RTSoft.SoureCode.Control.Bac;
using RTSoft.Modules;
using DevExpress.XtraCharts.Native;
using RTSoft.SoureCode.Control.City;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmNhanVien : DevExpress.XtraEditors.XtraForm
    {
       
        public frmNhanVien()
        {
            InitializeComponent();
        }
        //RepositoryItemGridLookUpEdit riCity; 
        DataTable tbl;
        String errConnect;
        String errSQL;
       
        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }

        public void InitGrid()
        {
            
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            nhanvienctrl ctrl = new nhanvienctrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["ID_NV"].Caption = "ID";
            GridView1.Columns["ID_NHOM"].Caption = "Chức Vụ";
            GridView1.Columns["ID_BAC"].Caption = "Trình Độ";
            GridView1.Columns["TEN_NV"].Caption = "Họ Và Tên";
            GridView1.Columns["USERNAME"].Caption = "Tài Khoản";
            GridView1.Columns["PASSWORD"].Caption = "Mật Khẩu";
            GridView1.Columns["PASSWORD"].Visible = false;
            GridView1.Columns["GIOITINH"].Caption = "Giới Tính";
            GridView1.Columns["TINH"].Caption = "Tỉnh thành";
            GridView1.Columns["TINH"].Visible = false;
            GridView1.Columns["HUYEN"].Caption = "Quận/Huyện";
            GridView1.Columns["HUYEN"].Visible = false;
            GridView1.Columns["DIACHI_NV"].Caption = "Địa Chỉ";
            //GridView1.Columns["DIACHI_NV"].OptionsColumn.AllowEdit = false;
            GridView1.Columns["SDT_NV"].Caption = "Số Điện Thoại";
            GridView1.Columns["TRANGTHAI"].Caption = "Trạng Thái";
            GridView1.Columns["NGAY_TAO"].Caption = "Ngày Tạo";
            GridView1.Columns["TRANGTHAI"].Visible = false;
            GridView1.Columns["ID_NV"].Visible = false;

            // Load danh sách nhóm quyền lên grid
            RepositoryItemGridLookUpEdit riCombo = new RepositoryItemGridLookUpEdit();
            nhomquyenctrl nctrl = new nhomquyenctrl();
            DataTable tblNhom = new DataTable();
            nctrl.getData(ref errConnect, ref errSQL, ref tblNhom);
            riCombo.DataSource = tblNhom;
            riCombo.ValueMember = "ID_NHOM";
            riCombo.DisplayMember = "TEN_NHOM";
            riCombo.NullText = "";
            riCombo.View.Columns["TEN_NHOM"].Caption = "Chức vụ";
            riCombo.View.Columns["ID_QUYEN"].Visible = false;
            riCombo.View.Columns["ID_NHOM"].Visible = false;
            riCombo.View.Columns["TRANGTHAI"].Visible = false;
            riCombo.View.Columns["NGAY_TAO"].Visible = false;
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_NHOM"].ColumnEdit = riCombo;

            RepositoryItemGridLookUpEdit riCity = new RepositoryItemGridLookUpEdit();
            cityctrl cityctrl = new cityctrl();
            DataTable tblCity = new DataTable();
            cityctrl.getData(ref errConnect, ref errSQL, ref tblCity);
            riCity.DataSource = tblCity;
            riCity.ValueMember = "locationId";
            riCity.DisplayMember = "locationName";
            riCity.NullText = "";
            riCity.View.Columns["locationName"].Caption = "Tỉnh thành";
            riCity.View.Columns["locationId"].Visible = false;
            riCity.View.Columns["parentId"].Visible = false;
            GridControl1.RepositoryItems.Add(riCity);
            GridView1.Columns["DIACHI_NV"].ColumnEdit = riCity;
            //riCity.EditValueChanged += new EventHandler(valueChange);

            //GridLookUpEdit riCity = sender as GridLookUpEdit;
            //int id = Convert.ToInt32(riCity.EditValue);
            //GridView1.SetFocusedRowCellValue("TINH", id);
            //RepositoryItemGridLookUpEdit riCombo = new RepositoryItemGridLookUpEdit();
            //cityctrl cityctrl = new cityctrl();
            //DataTable tblCity = new DataTable();
            //cityctrl.getDataByID(ref errConnect, ref errSQL, ref tblCity, id);
            //riCombo.DataSource = tblCity;
            //riCombo.ValueMember = "locationId";
            //riCombo.DisplayMember = "locationName";
            //riCombo.NullText = "";
            //riCombo.View.Columns["locationName"].Caption = "Quận/Huyện";
            //riCombo.View.Columns["locationId"].Visible = false;
            //riCombo.View.Columns["parentId"].Visible = false;
            //GridControl1.RepositoryItems.Add(riCombo);
            //GridView1.Columns["HUYEN"].ColumnEdit = riCity; 

            
            //Load danh sách trình độ (Bậc) lên grid
            riCombo = new RepositoryItemGridLookUpEdit();
            bacctrl bctrl = new bacctrl();
            DataTable tblBac = new DataTable();
            bctrl.getData(ref errConnect,ref errSQL, ref tblBac);
            riCombo.DataSource = tblBac;
            riCombo.ValueMember = "ID_BAC";
            riCombo.DisplayMember = "BAC";
            riCombo.NullText = "";
            riCombo.View.Columns["BAC"].Caption = "Tên Bậc";
            riCombo.View.Columns["ID_BAC"].Visible = false;
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_BAC"].ColumnEdit = riCombo;


            //Giới tính
            RepositoryItemComboBox ri = new RepositoryItemComboBox();
            ri.Items.Add("Nam");
            ri.Items.Add("Nữ");
            ri.Items.Add("Khác");
            GridControl1.RepositoryItems.Add(ri);
            GridView1.Columns["GIOITINH"].ColumnEdit = ri;

            // chỉnh cột ngày tạo thành dateTime
            RepositoryItemDateEdit riDate = new RepositoryItemDateEdit();
            riDate.ShowClear = true;
            riDate.ShowToday = true;
            riDate.EditMask = "dd/MM/yyyy";
            riDate.Mask.UseMaskAsDisplayFormat = true;
            GridView1.Columns["NGAY_TAO"].ColumnEdit = riDate;
            GridControl1.RepositoryItems.Add(riDate);
            GridView1.Columns["NGAY_TAO"].ColumnEdit = riDate;

            GridView1.Columns["TEN_NV"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            GridView1.BestFitColumns();	
           
        }

       


        //private void valueChange(object sender, EventArgs e)
        //{

        //    GridLookUpEdit riCity = sender as GridLookUpEdit;
        //    int id = Convert.ToInt32(riCity.EditValue);
        //    GridView1.SetFocusedRowCellValue("TINH", id);
        //    RepositoryItemGridLookUpEdit riCombo = new RepositoryItemGridLookUpEdit();
        //    cityctrl cityctrl = new cityctrl();
        //    DataTable tblCity = new DataTable();
        //    cityctrl.getDataByID(ref errConnect, ref errSQL, ref tblCity, id);
        //    riCombo.DataSource = tblCity;
        //    riCombo.ValueMember = "locationId";
        //    riCombo.DisplayMember = "locationName";
        //    riCombo.NullText = "";
        //    riCombo.View.Columns["locationName"].Caption = "Quận/Huyện";
        //    riCombo.View.Columns["locationId"].Visible = false;
        //    riCombo.View.Columns["parentId"].Visible = false;
        //    GridControl1.RepositoryItems.Add(riCombo);
        //    GridView1.Columns["HUYEN"].ColumnEdit = riCombo;   
        //}

        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            Library.ExportToExcel(this, "Danh Sách Nhân Viên", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView1.FocusedRowHandle >= 0)
                {
                    int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_NV"].FieldName));
                    errConnect = "";
                    errSQL = "";
                    string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["TEN_NV"].FieldName).ToString();
                    Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa " + strName + " ?");
                    if (result == false)
                    {
                        return;
                    }
                    nhanvienctrl ctrl = new nhanvienctrl();
                    ctrl.Delete(ref errConnect, ref errSQL, id);
                    if (errSQL != "")
                    {
                        Library.ShowMessage("Không thể xóa nhân viên này");
                    }
                    else
                    {
                        GridView1.DeleteSelectedRows();
                        GridView1.RefreshData();
                    }
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            String colTemp = "";
            try
            {
                // Bắt lỗi dữ liệu rỗng
                for (int i = 0; i < GridView1.Columns.Count - 1; i++) {
                    if (GridView1.Columns[i].Visible == true) {
                        colTemp = (GridView1.Columns[i].Name).Substring(3);
                        Object value = GridView1.GetRowCellValue(e.RowHandle, colTemp);
                        if ((colTemp == "ID_NHOM") || (colTemp == "ID_BAC") || (colTemp == "TEN_NV") || (colTemp == "GIOITINH") || (colTemp == "USERNAME"))
                        {
                            if (value == System.DBNull.Value)
                            { 
                            e.ErrorText = "Hãy Nhập " + GridView1.Columns[i].Caption;
                            e.Valid = false;
                            GridView1.FocusedColumn = GridView1.Columns[colTemp];
                            GridView1.ShowEditor();
                            return;
                            }
                        }
                    }
                }


                nhanvienctrl ctrl = new nhanvienctrl();
                nhanvieninfo info = new nhanvieninfo();
                errConnect = "";
                errSQL = "";
                DataRow row = GridView1.GetDataRow(e.RowHandle);
                info.ID_NV = Convert.ToInt32(RTValidate.CheckNull(row["ID_NV"]));
                info.ID_NHOM = Convert.ToInt32(RTValidate.CheckNull(row["ID_NHOM"]));
                info.ID_BAC = Convert.ToInt32(RTValidate.CheckNull(row["ID_BAC"]));
                //checkNull kiểm tra dữ liệu số nếu rỗng sẽ trả về 0
                info.TEN_NV = RTValidate.CheckNull(row["TEN_NV"]).ToString();
                info.USERNAME = RTValidate.CheckNull(row["USERNAME"]).ToString();
                info.PASSWORD = "";
                info.GIOITINH = RTValidate.CheckNull(row["GIOITINH"]).ToString();
                //checkNull kiểm tra dữ liệu chuỗi nếu rỗng sẽ trả về kí tự "khoảng trắng"
                info.TINH = "";
                info.HUYEN = "";
                info.DIACHI_NV = RTValidate.EmptyNull(row["DIACHI_NV"]).ToString();
                info.SDT_NV = RTValidate.CheckNull(row["SDT_NV"]).ToString();
                info.TRANGTHAI = 1;
                row["NGAY_TAO"] = DateTime.Now;
                info.NGAY_TAO = Convert.ToDateTime(RTValidate.CheckNull(row["NGAY_TAO"]));
                if (info.ID_NV != 0)
                {
                    Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi bạn có muốn sửa");
                    if (result != false)
                    {
                        ctrl.Update(ref errConnect, ref errSQL, info);
                    }
                }
                else
                {
                    ctrl.Insert(ref errConnect, ref errSQL, info);
                }
                this.InitGrid();
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }
        //Sự kiện chuyển tab xem thông tin chi tiết
        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            try
            {
                int iRow = this.GridView1.FocusedRowHandle;
                if (iRow >= 0)
                {
                    txtID.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_NV");
                    txtJobTittle.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_NHOM");
                    txtLevel.Text = this.GridView1.GetRowCellDisplayText(iRow, "ID_BAC");
                    txtName.Text = this.GridView1.GetRowCellDisplayText(iRow, "TEN_NV");
                    txtUsername.Text = this.GridView1.GetRowCellDisplayText(iRow, "USERNAME");
                    txtSex.Text = this.GridView1.GetRowCellDisplayText(iRow, "GIOITINH");
                    txtAddress.Text = this.GridView1.GetRowCellDisplayText(iRow, "DIACHI_NV");
                    txtPhone.Text = this.GridView1.GetRowCellDisplayText(iRow, "SDT_NV");
                    dtpDate.DateTime = Convert.ToDateTime(this.GridView1.GetRowCellValue(iRow, "NGAY_TAO"));
                }else{
                    txtID.Text = "";
                    txtJobTittle.Text = "";
                    txtLevel.Text = "";
                    txtName.Text = "";
                    txtUsername.Text = "";
                    txtSex.Text = "";
                    txtAddress.Text = "";
                    txtPhone.Text = "";
                    dtpDate.EditValue = "";
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

       
        //private void GridView1_ShowingEditor(object sender, CancelEventArgs e)
        //{
        //    String city = RTValidate.EmptyNull(GridView1.GetFocusedRowCellValue("TINH")).ToString();
        //    if (city == "")
        //    {
        //        GridView1.Columns["HUYEN"].OptionsColumn.AllowEdit = false;

        //    }
        //    else {
        //        GridView1.Columns["HUYEN"].OptionsColumn.AllowEdit = true;
        //        //Load danh sách huyện lên
                 
        //    }
        //}

       

      

        
    
    }
}